/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.restaurant.hellpers;

import com.restaurant.entities.Orders;
import com.restaurant.entities.Products;
import com.restaurant.entities.ProductsInOrders;
import com.restaurant.facade.OrdersFacadeLocal;
import com.restaurant.facade.ProductsInOrdersFacadeLocal;
import com.restaurant.jsf.RestaurantRatingsController;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author Dexter
 */
@ManagedBean
@SessionScoped
public class BasketBean implements Serializable{
    
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    private BigDecimal price = new BigDecimal(0);
    private boolean flgOrder = false;
    private Orders order;
    private List<Products> products;
    private DataModel items = null;
    
    @EJB
    private OrdersFacadeLocal ejbOrderFacade;
    @EJB
    private ProductsInOrdersFacadeLocal ejbProductFacade;
    
    @ManagedProperty(value = "#{restaurantRatingsController}")
    private RestaurantRatingsController restaurantRatingsController;

    public void setRestaurantRatingsController(RestaurantRatingsController restaurantRatingsController) {
        this.restaurantRatingsController = restaurantRatingsController;
    }
    
    /**
     * Creates a new instance of BasketBean
     */
    public BasketBean() {
    }
    
    public Orders getOrder() {
        return order;
    }

    public void setOrder(Orders order) {
        //Create new order
        this.order = order;
        flgOrder = true;
        price = new BigDecimal(0);
        products = new ArrayList<Products>();
    }

    public List<Products> getProducts() {
        return products;
    }

    public void setProducts(List<Products> products) {
        this.products = products;
    }
    
    public String getPrice() {
        return nf.format(price);
    }

    public boolean isFlgOrder() {
        return flgOrder;
    }
    
    public String prepareOrder(){
        items = new ListDataModel(products);
        return "clientBasket";
    }
    
    public void addProduct(Products prod){
        if(products == null)
            products = new ArrayList<Products>();
        
        products.add(prod);
        price = price.add(prod.getPrice());
    }

    public String removeProduct(){
        Products p = (Products) items.getRowData();
        products.remove(p);
        price = price.subtract(p.getPrice());
        items = new ListDataModel(products);
        return null;
    }
    
    public String saveOrder(){
        ejbOrderFacade.create(order);
        
        for (Iterator<Products> it = products.iterator(); it.hasNext();) {
            ProductsInOrders p = new ProductsInOrders();
            p.setOrderId(order);
            p.setProductId(it.next());
            ejbProductFacade.create(p);
        }
        
        cancelOrder();
        return restaurantRatingsController.prepareCreate();
    }
    
    public String cancelOrder(){
               
        order = null;
        flgOrder = false;
        price = new BigDecimal(0);
        products = null;
        
        return "index";
    }
    
    public DataModel getItems() {
        return items;
    }
    
    
}
